package com.mew.slots.core.game.dwsl.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.common.util.GameUtil;
import com.mew.slots.core.common.messge.response.SlotsBonusPlayResponseMessage;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.dwsl.bo.DwslInRoomList;
import com.mew.slots.core.game.dwsl.message.DwslBonusPlayResponseMessage;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DwslBonusService {
    private static final int[] ITEM={2,5,10,15,20,-1,-2,-3,-4};//选项
    private static final int[] ITEM_WEIGHTS={50,50,300,300,300,10,5,2,1};//选项权重
    public SlotsBonusPlayResponseMessage play(StrategyGameVO player){
        SlotsBonusPlayResponseMessage message=new SlotsBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsNumber() < 1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }
        if(player.getOtherMsg().length != 1){
            player.setStat(ResponseCode.BounsParamInvalid.code());
            return message;
        }
        int open = player.getOtherMsg()[0];
        DwslInRoomList vo = JSON.parseObject(player.getWinFree().getJson(), DwslInRoomList.class);
        for (int[] o : vo.getOpens()) {
            if(o[0] == open){
                player.setStat(ResponseCode.BounsParamInvalid.code());
                return message;
            }
        }
        player.setWinType(SlotsWinType.Special.code());
        player.setInBetNumber(winFree.getBet());

        DwslBonusPlayResponseMessage bonusPlayResponseMessage = playGame(player,vo);

        winFree.setBounsNumber(winFree.getBounsNumber()-1);
        if(winFree.getBounsNumber() == 0){
            vo.getOpens().clear();
        }
        winFree.setJson(JSON.toJSONString(vo));
        bonusPlayResponseMessage.setBonusNum(winFree.getBounsNumber());
        return bonusPlayResponseMessage;
    }

    /**
     * @param player
     * @return
     */
    private DwslBonusPlayResponseMessage playGame(StrategyGameVO player,DwslInRoomList vo){
        DwslBonusPlayResponseMessage message = new DwslBonusPlayResponseMessage();
        int randomIndex = GameUtil.weights(ITEM_WEIGHTS);
        int randomResult = ITEM[randomIndex];
        if(randomResult < 0) {// 结果特殊
            int type = Math.abs(randomResult);
            message.setType(type);
            message.setJpNo(type);
            player.setWinType(SlotsWinType.Jackpot.code());
        }else{
            message.setType(0);
            message.setRate(randomResult);
            message.setPayment(player.getWinFree().getBet() * player.getLineNumber() * randomResult);
            player.setPaymentNumber(message.getPayment());
        }
        vo.getOpens().add(new int[]{player.getOtherMsg()[0],randomResult > 0 ? (int)player.getPaymentNumber() : randomResult});
        return message;
    }
}
